<?php

namespace App\Services\Report;

use App\Model\User;
use App\Utils\MongoDBClient;
use function Hyperf\Translation\trans;

class TipOffService
{

    /**
     * Note - 举报
     * DateTime 2024/8/8 10:59
     * User Admin
     * @param mixed $userInfo
     * @param array $all
     */
    public function create(mixed $userInfo, array $all)
    {
        $user_id = $all['user_id']; //被举报人
        $user_reason = trim($all['user_reason']); //举报理由
        $extend = trim($all['extend'] ?? '');// 补充信息，可空
        $image_url = trim($all['image_url'] ?? '');
        $phone = $all['phone'] ?? ''; // 手机号码
        $from_page = $all['from_page'];    //来源页面 1.语音通话 2.聊天 3.个人资料 4.视频通话

        $monDB = MongoDBClient::getInstance();

        $insertData = array(
            'uid' => intval($userInfo['id']),
            'user_id' => intval($user_id),
            'type' => 1,  //和之前的举报数据做下区分
            'content' => $user_reason,
            'extend' => $extend,
            'phone' => $phone,
            'url' => $image_url,  //截图
            'ctime' => time(),
            'from' => intval($all['from'] ?? 0),
            'version_code' => intval($all['version_code'] ?? 0),
            'channel' => trim($all['channel'] ?? 'demo'),
            'check_result' => '0',
            'from_page' => strval($from_page)
        );
        $monDB->insert('tb_tipoff', $insertData);
        return formatData('', 200, 200, '提交成功');
    }


    /**
     * Note - 举报记录
     * DateTime 2024/8/8 11:38
     * User Admin
     * @param $userInfo
     * @param $all
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function journal($userInfo, $all)
    {
        $uid = $userInfo['id'];
        $language = $all['language'] ?? 'en';
        $type = $all['type']; // 1我举报的 2举报我的
        $page = $all['page'] ?? 1;

        $monDB = MongoDBClient::getInstance();
        $report_num = $monDB->getCount('tb_tipoff', ['uid' => intval($uid), 'type' => 1]); // 我举报的数量
        $be_report_num = $monDB->getCount('tb_tipoff', ['user_id' => intval($uid), 'type' => 1]);// 被我举报的数量


        $skip = ($page - 1) * 20;
        $options = array(
            'projection' => ['_id' => 0, 'uid' => 1, 'user_id' => 1, 'content' => 1, 'extend' => 1, 'ctime' => 1, 'check_result' => 1, 'from_page' => 1],
            'skip' => $skip,
            'limit' => 20,
            'sort' => array('ctime' => -1)
        );
        if ($type == 1) {
            $where = ['uid' => intval($uid), 'type' => 1];
        } else {
            $where = ['user_id' => intval($uid), 'type' => 1];
        }

        $result = $monDB->find('tb_tipoff', $where, $options);

        if (!empty($result)) {
            // 被举报用户ID
            $uids = array_column($result, $type == 1 ? "user_id" : "uid");
            // $uids = implode(",", $uids);
            // $users = (new UserModel())->findAll("id,nickname,head_image,sex,age", "id in ($uids)",[]);
            // $users = array_column($users, null, "id");
            $users = User::query()->select('id', 'nickname', 'head_image', 'sex', 'age')->whereIn('id', $uids)->get()->keyBy('id')->toArray();
            foreach ($result as &$re) {
                $content = explode(',', $re['content']);
                $newContent = '';
                foreach ($content as $c) {
                    $newContent .= ',' . trans('tipoff.' . $c, [], $language);
                }
                $re['content'] = trim($newContent, ',');
                $re['from_page'] = strval($re['from_page'] ?? 0);
                $re['user'] = $users[$re[$type == 1 ? 'user_id' : 'uid']] ?? [];
            }
        }

        // Tool::JSON(\CodeData::SUCCESS, array('result' => 1, 'msg' => $result, 'report_num' => $report_num, "be_report_num" => $be_report_num), 'success');
        return formatData(['result' => 1, 'msg' => $result, 'report_num' => $report_num, "be_report_num" => $be_report_num]);
    }

}